package cn.wolfcode.shop.cloud.web.controller;

import cn.wolfcode.shop.cloud.redis.RedisService;
import cn.wolfcode.shop.cloud.util.SeckillKeyPrefix;
import cn.wolfcode.shop.cloud.util.VerifyCodeImgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

@RestController
@RequestMapping("verifyCode")
public class VerifyCodeController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("initVerifCodeImg")
    public void initVerifCodeImg(String uuid, HttpServletResponse response) throws IOException {
        //生成验证码
        String verifyCode = VerifyCodeImgUtil.generateVerifyCode();
        //把表达式计算出来
        Integer calc = VerifyCodeImgUtil.calc(verifyCode);
        //把验证码存入redis
        redisService.set(SeckillKeyPrefix.SECKILL_VERIFY_CODE, uuid, calc);
        //生成图片
        BufferedImage verifyCodeImg = VerifyCodeImgUtil.createVerifyCodeImg(verifyCode);
        ImageIO.write(verifyCodeImg, "JPG", response.getOutputStream());
    }
}
